<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增乐理')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-theory-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">*乐理名称：</label>
                <div class="col-sm-8">
                    <input name="theoryName" class="form-control" type="text" placeholder="请输入乐理名称" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">*乐理级别：</label>
                <div class="col-sm-8">
                    <select name="theoryLevel" id="theoryLevel" class="form-control m-b" th:with="type=${@dict.getType('sys_level')}" placeholder="请选择乐理级别" required>
                        <option>请选择乐理级别：</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            
            <div class="form-group">
	            	<label class="col-sm-3 control-label">*乐理图片：</label>
		            <div class="col-sm-6">
	                    <input class="form-control" type="file" name="file" id="file" accept="image/*">
	                </div>
	                <div class="col-sm-2">
	                    <button class="btn btn-success fileinput-button" type="button" id="btnimage">上传</button>
	                </div>
	        </div>
	           
	        <div class="form-group" style="display:none;" id="imgDiv" name="imgDiv">
           		<label class="col-sm-3 control-label"></label>
           		<div class="col-sm-8">
           			<img class="img-square img-lg" name="imgUrl" id="imgUrl">
           		</div>
            </div>
	           
            <div class="form-group">    
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('sys_able_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script type="text/javascript">
        var prefix = ctx + "system/theory"
        $("#form-theory-add").validate({
            focusCleanup: true
        });

        $('#btnimage').click(function() {
            var val = $("#file").val();
            if (val.length==0) {
                layer.alert("未选择乐理图片");
                return ;
            }
            var formdata = new FormData();
            formdata.append("file", document.getElementById("file").files[0]);
            $.ajax({
                url: ctx + "system/upload/pictrue",
                data: formdata,
                type: "post",
                processData: false,
                contentType: false,
                success: function(result) {
                	$("#imgDiv").attr("style","display:block;");//显示div
                	$("#imgUrl").attr('src',result);
                	$("#file").val(result);//赋值
                }
            });
        });
        
        function submitHandler() {
            if ($.validate.form()) {
                var formdata = new FormData();
                formdata.append("theoryName", $("input[name='theoryName']").val());
                formdata.append("picture", $("#imgUrl")[0].src);
                var options=$("#theoryLevel option:selected");
                formdata.append("theoryLevel", options.val());
                formdata.append("status", $("input[name='status']").val());
                
                $.ajax({
                    url: prefix + "/add",
                    data: formdata,
                    type: "post",
                    processData: false,
                    contentType: false,
                    success: function(result) {
                    	$.operate.saveSuccess(result);
                    }
                })
            }
        }
    </script>
</body>
</html>